//
//  HouseLoanModel.m
//  SourceCodeRead
//
//  Created by Kael on 2018/7/1.
//  Copyright © 2018年 Kael. All rights reserved.
//

#import "HouseLoanModel.h"

@implementation HouseLoanModel

-(void)setPropsTotalPrice:(NSNumber *)totalPrice loanAmount:(NSNumber *)loanAmount mortgageYears:(NSNumber *)mortgageYears interest:(NSNumber *)interest repaymentMethod:(RepayMentMethod)repaymentMethod{
    self.totalPrice = totalPrice;
    self.loanAmount = loanAmount;
    self.mortgageYears = mortgageYears;
    self.interest = interest;
    self.repaymentMethod = repaymentMethod;
    
    // 这里自动计算下  更新属性后需要重新调一下这个方法
    [self calculateProps];
}

-(void)calculateProps{
    if (_repaymentMethod == RepayMentMethodEqualInterest) {
        
        if (_monthRepaymentsArr.count > 0) {
            [_monthRepaymentsArr removeAllObjects];
            [_monthInterestsArr removeAllObjects];
            [_monthPrincipalsArr removeAllObjects];
        }else{
            _monthRepaymentsArr = [NSMutableArray new];
            _monthPrincipalsArr = [NSMutableArray new];
            _monthInterestsArr = [NSMutableArray new];
        }
        
        /**
         每月还款数额计算公式如下：
         [贷款本金×月利率×（1+月利率）^还款月数]÷[（1+月利率）^还款月数－1]
         */
        for (int i=0; i<[_mortgageYears integerValue]*12; i++) {
            // 已还本金
            CGFloat repaymentPrincipalAmount = 0;
            for (NSNumber *monthPrincipal in self.monthPrincipalsArr) {
                repaymentPrincipalAmount = repaymentPrincipalAmount + [monthPrincipal floatValue];
            }
            
           
            // 月还款总额
            CGFloat fenzi = [_loanAmount floatValue] * [_interest floatValue] * pow((1 + [_interest floatValue]),([_mortgageYears integerValue] * 12.0));
            
            CGFloat fenmu = pow((1 + [_interest floatValue]),([_mortgageYears integerValue] * 12.0)) - 1;
            CGFloat monthRepayment = fenzi/fenmu;
           
            // 月还款利息
            CGFloat monthInterest = ([_loanAmount floatValue] - repaymentPrincipalAmount) * [_interest floatValue];
            // 月还款本金
            CGFloat monthPrincipal = monthRepayment - monthInterest;
            
            CGFloat remainingLoanAmount = [_loanAmount floatValue] - repaymentPrincipalAmount;
            // ---- 记录📝
            //每月总付款
            [self.monthRepaymentsArr addObject:@(monthRepayment)];
            // 每月利息
            [self.monthInterestsArr addObject:@(monthInterest)];
            // 没有本金
            [self.monthPrincipalsArr addObject:@(monthPrincipal)];
            // 每月剩余
            [self.monthRemainingsArr addObject:@(remainingLoanAmount)];
            
        }
        
        
    }else{
        
        if (_monthRepaymentsArr.count > 0) {
            [_monthRepaymentsArr removeAllObjects];
            [_monthInterestsArr removeAllObjects];
            [_monthPrincipalsArr removeAllObjects];
        }else{
            _monthRepaymentsArr = [NSMutableArray new];
            _monthPrincipalsArr = [NSMutableArray new];
            _monthInterestsArr = [NSMutableArray new];
        }
        
        /**
         每月还款金额= （贷款本金/ 还款月数）+（本金 — 已归还本金累计额）×每月利率
         */
        
        for (int i=0; i<[_mortgageYears integerValue] * 12; i++) {
            // 已还本金
            CGFloat repaymentPrincipalAmount = 0;
            
            // 月还款本金
            CGFloat monthPrincipal = [_loanAmount floatValue]/([_mortgageYears integerValue] * 12.0);
            if (i>0) {
                repaymentPrincipalAmount = i*monthPrincipal;
            }
            // 月还款利息
            CGFloat monthInterest = ([_loanAmount floatValue] - repaymentPrincipalAmount)*[_interest floatValue];
            // 月还款 本金 + 利息
            CGFloat monthRepayment = monthPrincipal + monthInterest;
            
            CGFloat remainingLoanAmount = [_loanAmount floatValue] - repaymentPrincipalAmount;
            // ---- 记录
            //每月总付款
            [self.monthRepaymentsArr addObject:@(monthRepayment)];
            // 每月利息
            [self.monthInterestsArr addObject:@(monthInterest)];
            // 每月本金
            [self.monthPrincipalsArr addObject:@(monthPrincipal)];
            // 每月剩余
            [self.monthRemainingsArr addObject:@(remainingLoanAmount)];
        }
    }
}


-(NSNumber *)interestAmount{
    CGFloat interests = 0;
    
    for (NSNumber *interest in self.monthInterestsArr) {
        interests = interests + [interest floatValue];
    }
    return @(interests);
}

-(NSNumber *)firstMonthPayment{
    if (self.monthInterestsArr && self.monthInterestsArr.count > 0) {
        return self.monthRepaymentsArr.firstObject;
    }
    return @0;
}


-(NSNumber *)dedreaseOfMonth{
    if (_repaymentMethod == RepayMentMethodEqualPrincipal) {
        if (self.monthRepaymentsArr && self.monthRepaymentsArr.count > 1) {
            
            NSNumber *first = self.monthRepaymentsArr[0];
            NSNumber *second = self.monthRepaymentsArr[1];
            
            return @([first floatValue] - [second floatValue]);
        }
    }
    return @0;
}

@end
